package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xebest.api.dto.XeVenLogisticsDetailResponseDTO;
import com.xebest.api.dto.XeVenLogisticsInfoResponseDTO;
import com.xebest.api.dto.XeVenOrderLogisticsRequestDTO;
import com.xebest.common.base.controller.BaseController;
import com.xebest.constant.XeErrorCode;
import com.xebest.web.form.XeVenOrderLogisticsForm;
import com.xebest.web.model.XeVenOrderLogistics;
import com.xebest.web.model.XeVenOrderLogisticsDetail;
import com.xebest.web.service.XeVenOrderEnteringLogisticsService;
import com.xebest.web.service.XeVenOrderLogisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 二方包里的物流信息接口
 *
 * @author 杜春静
 */
@RestController
@RequestMapping("restapi/logistic")
public class XeVenLogisticRestApi extends BaseController {

    private Logger log = LoggerFactory.getLogger(XeVenLogisticRestApi.class);

    @Autowired
    private XeVenOrderLogisticsService logisticService;

    @Autowired
    private XeVenOrderEnteringLogisticsService enteringLogisticsService;

    /**
     * 查询订单的物流详情
     *
     * @author 杜春静
     */
    @RequestMapping("/detailList")
    public List<XeVenLogisticsDetailResponseDTO> queryLogisticDetailList(
            @RequestBody XeVenOrderLogisticsRequestDTO logisticRequestDTO) {
        List<XeVenLogisticsDetailResponseDTO> logisticDetailList = new ArrayList<XeVenLogisticsDetailResponseDTO>();
        try {
            String incomParamJson = JSONObject.toJSONString(logisticRequestDTO);
            log.info("物流详情查询接口传入参数： {}", incomParamJson);
            if (StringUtils.isEmpty(logisticRequestDTO.getOrderId())) {
                return logisticDetailList;
            }
            List<XeVenOrderLogisticsDetail> detailList = logisticService
                    .listLogisticsDetail(logisticRequestDTO.getOrderId(), logisticRequestDTO.getCreateDateSort());
            if (detailList != null && detailList.size() > 0) {
                String detailListStr = JSONArray.toJSONString(detailList);
                logisticDetailList = JSONArray.parseArray(detailListStr, XeVenLogisticsDetailResponseDTO.class);
            }
        } catch (Exception e) {
            log.error("鲜易云ERP查询物流详情接口失败：{}", e);
            return logisticDetailList;
        }
        return logisticDetailList;
    }

    /**
     * 查询订单的物流信息包括物流详情
     *
     * @author 杜春静
     */
    @RequestMapping("/queryLogisticInfo")
    public XeVenLogisticsInfoResponseDTO queryLogisticInfo(
            @RequestBody XeVenOrderLogisticsRequestDTO logisticRequestDTO) {
        List<XeVenLogisticsDetailResponseDTO> logisticDetailList = new ArrayList<XeVenLogisticsDetailResponseDTO>();
        XeVenLogisticsInfoResponseDTO logisticInfo = new XeVenLogisticsInfoResponseDTO();
        List<XeVenOrderLogisticsDetail> detailList = new ArrayList<XeVenOrderLogisticsDetail>();
        try {
            String incomParamJson = JSONObject.toJSONString(logisticRequestDTO);
            log.info("物流详情查询接口传入参数： {}", incomParamJson);
            if (StringUtils.isEmpty(logisticRequestDTO.getOrderId())) {
                return sendApiError(logisticRequestDTO.getSpanId(), logisticInfo);
            }

            XeVenOrderLogisticsForm logisticsForm = new XeVenOrderLogisticsForm();
            logisticsForm.setOrderId(logisticRequestDTO.getOrderId());

            List<XeVenOrderLogistics> logisticsList = logisticService.selectLogisticsList(logisticsForm);
            if (logisticsList != null && logisticsList.size() > 0) {
                XeVenOrderLogistics logistic = logisticsList.get(0);
                String logisticStr = JSONObject.toJSONString(logistic);
                logisticInfo = JSONObject.parseObject(logisticStr, XeVenLogisticsInfoResponseDTO.class);
                if (logistic != null) {

                    //判断create_logistic_type(0.自动录入 1.手工录入生效 2.手工录入未生效',)是否是1,如果是1,则去手动录入的物流信息

                    Integer createLogisticType = enteringLogisticsService.queryOrderLogisticsCreateLogisticType(logisticRequestDTO.getOrderId());
                    if (createLogisticType != null && createLogisticType == 1) {
                        //不展示派车信息(新加的需求)
                        logisticInfo.setDriverCarNumber(null);
                        logisticInfo.setDriverPhone(null);

                        detailList = enteringLogisticsService.queryEnteringLogisticsDetailListByOrderId(logisticRequestDTO.getOrderId());
                    } else {
                        detailList = logisticService.listLogisticsDetail(
                                logisticRequestDTO.getOrderId(), logisticRequestDTO.getCreateDateSort());
                    }

                    if (detailList != null && detailList.size() > 0) {
                        String detailListStr = JSONArray.toJSONString(detailList);
                        logisticDetailList = JSONArray.parseArray(detailListStr, XeVenLogisticsDetailResponseDTO.class);
                    }
                    logisticInfo.setLogisticDetail(logisticDetailList);
                }

            }

        } catch (Exception e) {
            log.error("鲜易云ERP查询物流详情接口失败：{}", e);
            return sendApiError(logisticRequestDTO.getSpanId(), logisticInfo);
        }
        return logisticInfo;
    }

    /**
     * 查询一条最新的物流信息
     *
     * @author 杜春静
     */
    @RequestMapping("/latestLogistic")
    public XeVenLogisticsDetailResponseDTO latestLogistic(
            @RequestBody XeVenOrderLogisticsRequestDTO logisticRequestDTO) {
        XeVenLogisticsDetailResponseDTO logisticDetail = new XeVenLogisticsDetailResponseDTO();
        XeVenOrderLogisticsDetail detail = new XeVenOrderLogisticsDetail();
        logisticDetail.setStatus(XeErrorCode.PORJECT_ERROR);
        try {
            String incomParamJson = JSONObject.toJSONString(logisticRequestDTO);
            log.info("物流详情查询接口传入参数： {}", incomParamJson);
            if (StringUtils.isEmpty(logisticRequestDTO.getUserId())) {
                return sendApiError(logisticRequestDTO.getSpanId(), logisticDetail);
            }

            detail = enteringLogisticsService.getLatestLogistics(logisticRequestDTO.getUserId());

            if (detail == null) {
                detail = logisticService.getLatestLogistics(logisticRequestDTO.getUserId());
            }

            if (detail != null) {
                String detailStr = JSONObject.toJSONString(detail);
                logisticDetail = JSONObject.parseObject(detailStr, XeVenLogisticsDetailResponseDTO.class);
                logisticDetail.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
        } catch (Exception e) {
            log.error("鲜易云ERP查询物流详情接口失败：{}", e);
            return sendApiError(logisticRequestDTO.getSpanId(), logisticDetail);
        }
        return logisticDetail;
    }
}
